<?php
/* *
 * 功能：1号店接口调用模型
 * 版本：1.0
 * 日期：2015/5/15
 * 说明：
 * 以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己网站的需要，按照技术文档编写，并非一定要使用该代码。
 * 该代码仅供学习和研究提供一个参考。
 * 配置参数修改见第370行 - 第374行。
 */



//商品信息批量查询
class productListQuery extends yhd {

	public $productIdArray, $fieldsStr, $provinceId;
	private $requestBizData;

	function __construct()
	{
		parent::__construct();
	}

	function execute()
	{
		if (empty($this->fieldsStr))
		{
			return $this->error("ERROR: Parameter \"fieldStr\" is empty!");
		}
		if (!is_array($this->productIdArray) || empty($this->productIdArray))
		{
			return $this->error("ERROR: Parameter \"productIdArray\" is not an array or is empty!");
		}
		if (!is_numeric($this->provinceId))
		{
			return $this->error("ERROR: Parameter \"provinceId\" is not numeric!");
		}
		$this->requestBizData = array(
			"fields"        => trim($this->fieldsStr),
			"productIdList" => $this->productIdArray,
			"provinceId"    => intval($this->provinceId)
		);
		return $this->getReturn(parent::$productListInfo, $this->requestBizData);
	}
}


//商品信息查询
class productInfoQuery extends yhd {

	public $fieldsStr, $pageSize, $provinceId, $pageIndex, $updateTime;
	private $requestBizData;

	function __construct()
	{
		parent::__construct();
	}

	function execute()
	{
		if (empty($this->fieldsStr))
		{
			return $this->error("ERROR: Parameter \"fieldStr\" is empty!");
		}
		if (!is_numeric($this->pageSize))
		{
			return $this->error("ERROR: Parameter \"pageSize\" is not numeric!");
		}
		if (!is_numeric($this->provinceId))
		{
			return $this->error("ERROR: Parameter \"provinceId\" is not numeric!");
		}
		if (!is_numeric($this->pageIndex))
		{
			return $this->error("ERROR: Parameter \"pageIndex\" is not numeric!");
		}
		if (!is_numeric($this->updateTime))
		{
			return $this->error("ERROR: Parameter \"updateTime\" is not numeric!");
		}
		$this->requestBizData = array(
			"fields"        => trim($this->fieldsStr),
			"pageSize"      => $this->pageSize,
			"pageIndex"     => $this->pageIndex,
			"updateTime"    => $this->updateTime,
			"provinceId"    => intval($this->provinceId)
		);
		return $this->getReturn(parent::$productInfo, $this->requestBizData);
	}
}


//库存价格查询
class stockPriceQuery extends yhd {

	public $productIdArray, $provinceId;
	private $requestBizData;

	function __construct()
	{
		parent::__construct();
	}

	function execute()
	{
		if (!is_array($this->productIdArray) || empty($this->productIdArray))
		{
			return $this->error("ERROR: Parameter \"productIdArray\" is not an array or is empty!");
		}
		if (!is_numeric($this->provinceId))
		{
			return $this->error("ERROR: Parameter \"provinceId\" is not numeric!");
		}
		$this->requestBizData = array(
			"productIdList" => $this->productIdArray,
			"provinceId"    => intval($this->provinceId)
		);
		return $this->getReturn(parent::$stockPrice, $this->requestBizData);
	}
}


//订单校验
class orderCheck extends yhd {

	public $receiver_id, $receiver_name, $receiver_province, $receiver_city, $receiver_county, $receiver_address, $receiver_mobile, $receiver_phone;
	public $tradeItemList = array();
	private $requestBizData;

	function __construct()
	{
		parent::__construct();
	}

	function execute()
	{
		if (!is_array($this->tradeItemList) || empty($this->tradeItemList))
		{
			return $this->error("ERROR: Parameter \"tradeItemList\" is not an array or is empty!");
		}
		$this->requestBizData = array(
			"trade" => array(
				"receiver" => array(
					"id" => $this->receiver_id,
					"name" => $this->receiver_name,
					"province" => $this->receiver_province,
					"city" => $this->receiver_city,
					"county" => $this->receiver_county,
					"address" => $this->receiver_address,
					"mobile" => $this->receiver_mobile,
					"phone" => $this->receiver_phone
				),
				"tradeItemList" => $this->tradeItemList
			)
		);
		return $this->getReturn(parent::$orderCheck, $this->requestBizData);
	}
}


//订单生成
class orderCreate extends yhd {

	public $receiver_id, $receiver_name, $receiver_province, $receiver_city, $receiver_county, $receiver_address, $receiver_mobile, $receiver_phone;
	public $tradeItemList = array();
	public $thirdOrderCode, $paymentAmount, $orderDiscount, $productAmount, $invoiceName, $invoiceContent, $splitTicket, $splitSign, $splitTime, $fourthAreaId, $postFee;
	private $requestBizData;

	function __construct()
	{
		parent::__construct();
	}

	function execute()
	{
		if (!is_array($this->tradeItemList) || empty($this->tradeItemList))
		{
			return $this->error("ERROR: Parameter \"tradeItemList\" is not an array or is empty!");
		}
		$this->requestBizData = array(
			"trade" => array(
				"receiver" => array(
					"id" => $this->receiver_id,
					"name" => $this->receiver_name,
					"province" => $this->receiver_province,
					"city" => $this->receiver_city,
					"county" => $this->receiver_county,
					"address" => $this->receiver_address,
					"mobile" => $this->receiver_mobile,
					"phone" => $this->receiver_phone
				),
				"tradeItemList" => $this->tradeItemList,
				"thirdOrderCode"=> $this->thirdOrderCode,
				"paymentAmount"=> $this->paymentAmount,
				"orderDiscount"=> $this->orderDiscount,
				"productAmount"=> $this->productAmount,
				"invoiceName"=> $this->invoiceName,
				"invoiceContent"=> $this->invoiceContent,
				"splitTicket"=> $this->splitTicket,
				"splitSign"=> $this->splitSign,
				"splitTime"=> $this->splitTime,
				"fourthAreaId"=> $this->fourthAreaId,
				"postFee"=> $this->postFee
			)
		);
		return $this->getReturn(parent::$orderCreate, $this->requestBizData);
	}
}


//支付通知
class orderPaidNotify extends yhd {
	public $thirdOrderCodeStr, $yhdOrderCodeStr;
	private $requestBizData;
	function __construct()
	{
		parent::__construct();
	}
	function execute()
	{
		if (empty($this->thirdOrderCodeStr))
		{
			return $this->error("ERROR: Parameter \"thirdOrderCodeStr\" is empty!");
		}
		if (empty($this->yhdOrderCodeStr))
		{
			return $this->error("ERROR: Parameter \"yhdOrderCodeStr\" is empty!");
		}
		$this->requestBizData = array(
			"thirdOrderCode" => $this->thirdOrderCodeStr,
			"yhdOrderCode"    => $this->yhdOrderCodeStr
		);
		return $this->getReturn(parent::$payNotify, $this->requestBizData);
	}
}


//物流查询
class orderLogisticsQuery extends yhd {
	public $yhdOrderCodeListArr;
	private $requestBizData;
	function __construct()
	{
		parent::__construct();
	}
	function execute()
	{
		if (!is_array($this->yhdOrderCodeListArr) && empty($this->yhdOrderCodeListArr))
		{
			return $this->error("ERROR: Parameter \"yhdOrderCodeListArr\" is not an array or is empty!");
		}
		$this->requestBizData = array(
			"yhdOrderCodeList" => $this->yhdOrderCodeListArr
		);
		return $this->getReturn(parent::$orderLogis, $this->requestBizData);
	}
}


//订单状态查询
class orderStatusQuery extends yhd {
	public $yhdOrderCodeListArr, $fieldsStr;
	private $requestBizData;
	function __construct()
	{
		parent::__construct();
	}
	function execute()
	{
		if (!is_array($this->yhdOrderCodeListArr) && empty($this->yhdOrderCodeListArr))
		{
			return $this->error("ERROR: Parameter \"yhdOrderCodeListArr\" is not an array or is empty!");
		}
		if (empty($this->fieldsStr))
		{
			return $this->error("ERROR: Parameter \"fieldsStr\" is empty!");
		}
		$this->requestBizData = array(
			"yhdOrderCodeList" => $this->yhdOrderCodeListArr,
			"fields" => $this->fieldsStr
		);
		return $this->getReturn(parent::$orderStatus, $this->requestBizData);
	}
}


//财务对账
class accountChecking extends yhd {
	public $trdMerchantCode, $trdMerchantName, $billDate;
	public $billInfoList = array();
	private $requestBizData;
	function __construct()
	{
		parent::__construct();
	}
	function execute()
	{
		if (empty($this->trdMerchantCode))
		{
			return $this->error("ERROR: Parameter \"trdMerchantCode\" is empty!");
		}
		if (empty($this->trdMerchantName))
		{
			return $this->error("ERROR: Parameter \"trdMerchantName\" is empty!");
		}
		if (empty($this->billDate))
		{
			return $this->error("ERROR: Parameter \"billDate\" is empty!");
		}
		$this->requestBizData = array(
			"trdMerchantCode" => $this->trdMerchantCode,
			"trdMerchantName" => $this->trdMerchantName,
			"billDate" => $this->billDate,
			"billInfoList" => $this->billInfoList
		);
		return $this->getReturn(parent::$accountCheck, $this->requestBizData);
	}
}


//退货查询
class orderReturnQuery extends yhd {
	public $orderCodeStr;
	private $requestBizData;
	function __construct()
	{
		parent::__construct();
	}
	function execute()
	{
		if (empty($this->orderCodeStr))
		{
			return $this->error("ERROR: Parameter \"orderCodeStr\" is not an array or is empty!");
		}
		$this->requestBizData = array(
			"orderCode" => $this->orderCodeStr
		);
		return $this->getReturn(parent::$returnOrder, $this->requestBizData);
	}
}


//退货批量查询
class orderReturnListQuery extends yhd {
	public $startTime, $endTime, $pageIndex, $pagSize;
	private $requestBizData;
	function __construct()
	{
		parent::__construct();
	}
	function execute()
	{
		$this->requestBizData = array(
			"startTime" => $this->startTime,
			"endTime" => $this->endTime,
			"pageIndex" => $this->pageIndex,
			"pageSize" => $this->pagSize
		);
		return $this->getReturn(parent::$returnListOrder, $this->requestBizData);
	}
}




/*----------------基本模型-------------------*/
class yhd {

	private $appKey = "10000075";
	private $sessionKey = "4ab260447d1ace7f86e5f849b4b17451a018b1414d208c507a99e7ed";
	private $bizSource = "ZA_BXYP";
	private $version = "v1.0";
	private $baseUrl = "http://119.97.231.228:2010/b2b2c/rest";//网关

	protected static $productInfo = "/product/info/query";//商品信息接口
	protected static $productListInfo = "/product/list/query"; //商品批量查询信息
	protected static $orderCheck = "/order/check";//订单验证
	protected static $orderCreate = "/order/create";//订单生成
	protected static $payNotify = "/order/paid/notify";//订单支付通知
	protected static $orderStatus = "/order/status/query";//订单状态查询
	protected static $orderLogis = "/order/logistics/query";//物流查询
	protected static $stockPrice = "/product/stockprice/query";//库存价格查询
	protected static $accountCheck = "/account/checking";//财务对账
	protected static $returnOrder = "/order/return/query"; //退货查询
	protected static $returnListOrder = "/order/return/list/query"; //退货批量查询

	function __construct()
	{
	}

	protected function getReturn($port, $requestBizData)
	{
		$requestBizData = json_encode($requestBizData);
		$sign = array(
			"appKey" => $this->appKey,
			"bizSource" => $this->bizSource,
			"sessionKey" => $this->sessionKey,
			"version" => $this->version,
			"timestamp" => date("Y-m-d H:i:s"),//请确保服务器时间准确，否则需要调整
			"requestBizData" => $requestBizData
		);
		$sign = $this->createSign($sign);

		$header = array(
			"timestamp" => date("Y-m-d H:i:s"),//请确保服务器时间准确，否则需要调整
			"appKey" => $this->appKey,
			"sessionKey" => $this->sessionKey,
			"bizSource" => $this->bizSource,
			"version" => $this->version,
			"sign" => md5($sign)
		);

		$post_data = array(
			"header" => $header,
			"requestBizData" => $requestBizData
		);
		$post_data = json_encode($post_data);

		return $this->postData($port, $post_data);
	}

	private function postData($port, $post_data)
	{
		$url = $this->getUrl($port);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HEADER,0);
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
		$ret = curl_exec($ch);
		curl_close($ch);

		$ret = preg_replace("/	/" , "", $ret);
		$ret = json_decode($ret);
		return $ret;
	}

	private function createSign($sign)
	{
		if(!is_array($sign) || empty($sign))
		{
			$this->error();
		}
		$str = "";
		foreach($sign as $k=>$v)
		{
			$str .= $k.":".$v.",";
		}
		$str = substr($str, 0, -1);
		return $str;
	}

	protected function error($msg="")
	{
		if (empty($msg)) $msg = "ParameterError!";
		return $msg;
	}

	private function getUrl($tailUrl)
	{
		return $this->baseUrl.$tailUrl;
	}

	function __destruct()
	{
	}

}


?>